﻿using CoreCms.Net.Configuration;
using Jiguang.JPush.DependencyInjection;
using Jiguang.JPush.Model;
using Microsoft.Extensions.DependencyInjection;
using NPOI.SS.UserModel;
using Qc.YilianyunSdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CoreCms.Net.Core.Config
{
    /// <summary>
    /// 极光推送 启动服务
    /// </summary>
    public static class JiGuangPushSetup
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void AddJiGuangPushSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));

            services.AddJPush(options =>
            {
                options.AppKey = AppSettingsConstVars.JiGuangPushConfigAppKey;
                options.MasterSecret = AppSettingsConstVars.JiGuangPushConfigMasterSecret;
            });
        }
        /// <summary>
        /// 依据别名进行推送消息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="body">内容</param>
        /// <param name="lstAlias">别名集合</param>
        /// <param name="platform">推送平台 默认 ios android</param>
        /// <returns></returns>
        public static PushPayload GetAliasPayload(string title, string body, List<string> lstAlias, string platform = "android,ios")
        {
            if (title.Length > 20) body = title.Substring(0, 20);
            if (body.Length > 100) body = body.Substring(0, 100);
            PushPayload payload = new PushPayload()
            {
                Platform = platform.Split(','),
                Audience = new
                {
                    alias = lstAlias
                },
                Notification = new Notification()
                {
                    Alert = body,
                    Android = new Android()
                    {
                        Alert = body,
                        Title = title,
                        Indent = new Dictionary<string, object> { { "url", "intent:#Intent;action=android.intent.action.MAIN;end" } }
                    },
                    IOS = new IOS()
                    {
                        Alert = new
                        {
                            title = title,
                            body = body
                        },
                        Sound = "default",
                        ContentAvailable = true
                    }
                }
            };
            return payload;
        }

        /// <summary>
        /// 推送给所有人 每天限制5次 超出后推送失败
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="body">内容</param>
        /// <param name="platform">推送平台 默认 ios android</param>
        /// <returns></returns>
        public static PushPayload GetAllPayload(string title, string body, string platform = "android,ios")
        {
            if (title.Length > 20) body = title.Substring(0, 20);
            if (body.Length > 100) body = body.Substring(0, 100);
            PushPayload payload = new PushPayload()
            {
                Platform = platform.Split(','),
                Audience = "all",
                Notification = new Notification()
                {
                    Alert = body,
                    Android = new Android()
                    {
                        Alert = body,
                        Title = title,
                        Indent = new Dictionary<string, object> { { "url", "intent:#Intent;action=android.intent.action.MAIN;end" } }
                    },
                    IOS = new IOS()
                    {
                        Alert = new
                        {
                            title = title,
                            body = body
                        },
                        Sound = "default",
                        ContentAvailable = true
                    }
                }
            };
            return payload;
        }
    }
}
